package com.dong.thread.threadbasic;

import java.util.concurrent.CountDownLatch;

/**
 * 线程的原子性
 * 从一个简单的小程序谈起
 *
 * @author by jiweidong on 2021/11/26.
 */
public class T17_Atomicity_PlusPlus {

    private static long n = 0L;

    public static void main(String[] args) throws InterruptedException {

        Thread[] threads = new Thread[100];
        CountDownLatch latch = new CountDownLatch(threads.length);

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 10000; j++) {
                    //synchronized (T00_IPlusPlus.class) {
                    n++;
                    //}
                }
                latch.countDown();
            });
        }

        for (Thread t : threads) {
            t.start();
        }

        latch.await();

        System.out.println(n);
    }
}
